      1  import java.io.IOException;

      2  import java.sql.SQLException;

      3  import java.util.Scanner;

      4

      5  /**

      6     A text-based simulation of an automatic teller machine

      7  */

      8  public class ATMSimulator

      9  {

     10     public static void main(String[] args)

     11           throws IOException, SQLException, ClassNotFoundException

     12     {

     13        if (args.length == 0)

     14        {

     15           System.out.println(

     16                 "Usage: ATMTester propertiesFile");

     17           return;

     18        }

     19        else

     20        {

     21           SimpleDataSource.init(args[0]);

     22        }

     23

     24        Bank theBank = new Bank();

     25        ATM theATM = new ATM(theBank);

     26

     27        Scanner in = new Scanner(System.in);

     28

     29        while (true)

     30        {

     31           int state = theATM.getState();

     32           if (state == ATM.START)

     33           {

     34              System.out.print("Enter account number: ");

     35              int number = in.nextInt();

     36              theATM.setCustomerNumber(number);

     37           }

     38           else if (state == ATM.PIN)

     39           {

     40              System.out.print("Enter PIN: ");

     41              int pin = in.nextInt();

     42              theATM.selectCustomer(pin);

     43           }

     44           else if (state == ATM.ACCOUNT)

     45           {

     46              System.out.print("A=Checking, B=Savings, C=Quit: ");

     47              String command = in.next();

     48              if (command.equalsIgnoreCase("A"))

     49              {

     50                 theATM.selectAccount(ATM.CHECKING);

     51              }

     52              else if (command.equalsIgnoreCase("B"))

     53              {

     54                 theATM.selectAccount(ATM.SAVINGS);

     55              }

     56              else if (command.equalsIgnoreCase("C"))

     57              {

     58                 theATM.reset();

     59              }

     60              else

     61              {

     62                 System.out.println("Illegal input!");

     63              }

     64           }

     65           else if (state == ATM.TRANSACT)

     66           {

     67              System.out.println("Balance=" + theATM.getBalance());

     68              System.out.print("A=Deposit, B=Withdrawal, C=Cancel: ");

     69              String command = in.next();

     70              if (command.equalsIgnoreCase("A"))

     71              {

     72                 System.out.print("Amount: ");

     73                 double amount = in.nextDouble();

     74                 theATM.deposit(amount);

     75                 theATM.back();

     76              }

     77              else if (command.equalsIgnoreCase("B"))

     78              {

     79                 System.out.print("Amount: ");

     80                 double amount = in.nextDouble();

     81                 theATM.withdraw(amount);

     82                 theATM.back();

     83              }

     84              else if (command.equalsIgnoreCase("C"))

     85              {

     86                 theATM.back();

     87              }

     88              else

     89              {

     90                 System.out.println("Illegal input!");

     91              }

     92           }

     93        }

     94     }

     95  }

     96